package com.zhanglijie.improve.atomic.one;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntBinaryOperator;
import java.util.function.IntUnaryOperator;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/9/1 0001 0:57
 */
public class AutomicIntegerDemo {
    private int i = 0;
    AtomicInteger atomicInteger = new AtomicInteger(0);
    public static void main(String[] args) throws  Exception {
        AutomicIntegerDemo automicIntegerDemo = new AutomicIntegerDemo();
        // automicIntegerDemo.testOne();
       // automicIntegerDemo.testTwo();
        //automicIntegerDemo.testAutoFunc(new AtomicInteger(100));
        //automicIntegerDemo.testIntUnaryOperator();
        automicIntegerDemo.testIntBinaryOperator(new AtomicInteger(2));
    }

   public void testIntBinaryOperator(AtomicInteger atomicInteger){
       IntBinaryOperator opt = (x,y)->x*y;
       final int andAccumulate = atomicInteger.getAndAccumulate(3, opt);//3*2
       System.out.println(atomicInteger.get());
   }
    public void testIntUnaryOperator(){
        IntUnaryOperator intUnaryOperator = IntUnaryOperator.identity();
        int i = intUnaryOperator.applyAsInt(6);
        System.out.println(i);

        IntUnaryOperator intUnaryOperator1 = x->x+x;
        int my = intUnaryOperator1.applyAsInt(6);
        System.out.println(my);

        IntUnaryOperator intUnaryOperator2 = x->x+1;
        final IntUnaryOperator intUnaryOperator3 = intUnaryOperator2.andThen(x -> x * 2);
        System.out.println(intUnaryOperator3.applyAsInt(6));//14

        IntUnaryOperator intUnaryOperator4 = x->x+1;
        final IntUnaryOperator intUnaryOperator5 = intUnaryOperator4.compose(x -> x * 2);
        System.out.println(intUnaryOperator5.applyAsInt(6));//13

    }
    public void testAutoFunc(AtomicInteger atomicInteger){
        IntUnaryOperator intUnaryOperator = x->x*x;
        // final int i = atomicInteger.updateAndGet(intUnaryOperator);
        // System.out.println( i);
        final int andUpdate = atomicInteger.getAndUpdate(intUnaryOperator);
        System.out.println(andUpdate);
        System.out.println(atomicInteger.get());
    }
    public void testTwo() throws InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(2);
        new Thread(()->{
            for (int i1 = 0; i1 < 90000; i1++) {
                atomicInteger.incrementAndGet();
            }
            countDownLatch.countDown();
        }).start();
        new Thread(()->{
            for (int i1 = 0; i1 < 90000; i1++) {
                atomicInteger.incrementAndGet();
            }
            countDownLatch.countDown();
        }).start();
        countDownLatch.await();
        System.out.println( atomicInteger.get());//不一定是180000
    }
    public void testOne() throws InterruptedException {

        final CountDownLatch countDownLatch = new CountDownLatch(2);
        new Thread(()->{
            for (int i1 = 0; i1 < 90000; i1++) {
                i++;
            }
            countDownLatch.countDown();
        }).start();
        new Thread(()->{
            for (int i1 = 0; i1 < 90000; i1++) {
               i++;
            }
            countDownLatch.countDown();
        }).start();
        countDownLatch.await();
        System.out.println(i);//不一定是180000
    }
}
